﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;

namespace mamnontuoitho7.backend
{
    public partial class TeacherEdit : System.Web.UI.Page
    {

        protected Teacher _teacher = new Teacher();
        protected General _general = new General();
        private ThumbnailClass _thumbnailClass = new ThumbnailClass();
        private Class _class = new Class();
        protected DataRow _dtrTeacher;

        protected String _teacherID = "";
        protected String _teacherAvatar = "";

        protected int _teacherClassID;
        protected string _teacherClassCategory;

        protected void Page_Load(object sender, EventArgs e)
        {
            //Check Role Admin
            if (Session["memberID"].ToString() != "" && (ConstantClass.permissionInfo).IndexOf(Session["role"].ToString() + ",") != -1)
            {
                try
                {

                    if (Request.QueryString["hidTeacherID"] != null && Request.QueryString["hidNamePage"] != null)
                    {
                        _teacherID = Request.QueryString["hidTeacherID"].ToString();
                        hidNamePage.Value = Request.QueryString["hidNamePage"].ToString();

                        if (Request.QueryString["hidClassID"] != null && Request.QueryString["hidClassName"] != null)
                        {
                            hidClassID.Value = Request.QueryString["hidClassID"].ToString();
                            hidClassName.Value = Request.QueryString["hidClassName"].ToString();
                        }
                        if (!IsPostBack)
                        {
                            loadClassList();
                            loadTeacherInformation(_teacherID);
                        }
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorAdminPage);
                }
            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }

        /// <summary>
        /// Load teacher information from database
        /// </summary>
        private void loadTeacherInformation(String teacherID)
        {

            _dtrTeacher = _teacher.GetTeacherDetails(teacherID);
            txtMemberName.Text = _dtrTeacher["MemberName"].ToString();
            txtFullname.Text = _dtrTeacher["FullName"].ToString();
            ddlSex.SelectedValue = _dtrTeacher["Sex"].ToString();
            txtBirthday.Text = _dtrTeacher["Birthday"].ToString();
            txtAddress.Text = _dtrTeacher["Address"].ToString();
            txtEmail.Text = _dtrTeacher["Email"].ToString();

            txtClassCategory.Text = _dtrTeacher["ClassCategoryName"].ToString();
            txtPhone.Text = _dtrTeacher["Phone"].ToString();
            ddlTeacherClass.SelectedValue = _dtrTeacher["ClassID"].ToString();
            hidImageTeacher.Value = _teacherAvatar = _dtrTeacher["Avatar"].ToString();
            imgTeacher.ImageUrl = "../images/avatar/teacher_parents/" + (_teacherAvatar == "" ? ConstantClass._memberDefaultAvatar : _teacherAvatar);
            ddlTeacherVisible.SelectedValue = _dtrTeacher["MemberVisible"].ToString().ToLower();
        }

        /// <summary>
        /// Load Class List and put in Dropdownlist
        /// </summary>
        private void loadClassList()
        {
            DataTable dttClass = _class.GetClassList("0");
            ddlTeacherClass.DataSource = dttClass;
            ddlTeacherClass.DataTextField = "ClassName";
            ddlTeacherClass.DataValueField = "ClassID";
            ddlTeacherClass.DataBind();
        }


        /// <summary>
        /// Save update information of teacher
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnTeachertEdit_Click(object sender, EventArgs e)
        {
            try
            {

                if (Request.QueryString["hidTeacherID"] != null)
                {
                    _teacherID = Request.QueryString["hidTeacherID"].ToString();
                    _teacherAvatar = hidImageTeacher.Value;

                    if (Request.QueryString["hidClassID"] != null && Request.QueryString["hidClassName"] != null)
                    {
                        hidClassID.Value = Request.QueryString["hidClassID"].ToString();
                        hidClassName.Value = Request.QueryString["hidClassName"].ToString();
                    }


                    if (txtMemberName.Text.Trim() == "")
                    {
                        lblTeacherEditNotice.Visible = true;
                        lblTeacherEditNotice.Text = "Tên đăng nhập không được để trống.";
                        return;
                    }

                    string avatar = txtFullname.Text.Trim() + "-" + _teacherID;
                    if (fulTeacherAvatar.FileName != "" && fulTeacherAvatar.FileName != null)
                    {
                        if (fulTeacherAvatar.FileBytes.Count() / 1024 > 6144)
                        {
                            lblTeacherEditNotice.Visible = true;
                            lblTeacherEditNotice.Text = "Dung lượng hình ảnh phải dưới 6MB.";
                            return;
                        }

                        avatar = _thumbnailClass.UploadImage2(fulTeacherAvatar, Request.PhysicalApplicationPath + "images/avatar/teacher_parents/", avatar, 128, 128, _teacherAvatar);

                        if (avatar == "")
                        {
                            lblTeacherEditNotice.Visible = true;
                            lblTeacherEditNotice.Text = "Hình của giáo viên mới không thể upload được. File hình chỉ hỗ trợ những file: jpg, jpeg, gif, png, bmp, tiff";
                            return;
                        }
                    }
                    else
                    {
                        avatar = _teacherAvatar;
                    }

                    int checkUpdate = _teacher.UpdateTeacher(txtMemberName.Text.Trim(),
                                                              _teacherID,
                                                              txtFullname.Text.Trim(),
                                                              ddlSex.SelectedValue,
                                                              txtBirthday.Text.Trim(),
                                                               txtAddress.Text.Trim(),
                                                              txtEmail.Text.Trim(), txtPhone.Text.Trim(),
                                                              avatar,
                                                              ddlTeacherVisible.SelectedValue,
                                                              ddlTeacherClass.SelectedValue);


                    if (checkUpdate == 1)
                    {
                        lblTeacherEditNotice.Visible = true;
                        lblTeacherEditNotice.Text = "Thông tin của giáo viên đã được cập nhật.";
                        hidImageTeacher.Value = _teacherAvatar = avatar;
                        imgTeacher.ImageUrl = "../images/avatar/teacher_parents/" + (avatar == "" ? ConstantClass._memberDefaultAvatar : avatar) + "?" + DateTime.Now.Ticks;

                    }
                    else if (checkUpdate == ConstantClass.duplicateKey)
                    {
                        lblTeacherEditNotice.Visible = true;
                        lblTeacherEditNotice.Text = "Tên đăng nhập này đã có. Xin vui lòng đổi tên khác.";
                    }
                    else
                    {
                        lblTeacherEditNotice.Visible = true;
                        lblTeacherEditNotice.Text = "Có lỗi hệ thống không thể cập nhật được.";
                    }


                    //Response.Redirect("TeacherEdit.aspx");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }
        }
        /// <summary>
        /// Reload teacher information
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCancle_Click(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["hidTeacherID"] != null)
                {
                    _teacherID = Request.QueryString["hidTeacherID"].ToString();
                    loadClassList();
                    loadTeacherInformation(_teacherID);
                    lblTeacherEditNotice.Visible = false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }
        }


        /// <summary>
        /// Reset pass to default pass for member
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnResetDefaultPass_Click(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["hidTeacherID"] != null)
                {
                    _teacherID = Request.QueryString["hidTeacherID"].ToString();
                    _teacher.ResetDefaultPass(_teacherID);
                    lblTeacherEditNotice.Visible = true;
                    lblTeacherEditNotice.Text = "Mật khẩu của giáo viên " + txtFullname.Text.Trim() + " đã được đổi thành là " + ConstantClass._passwordDefault;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }
        }

    }
}